home *** CD-ROM | disk | FTP | other *** search
- {**************************************************************************************
- * *
- * Module Name : FILES.PAS *
- * Type : UNIT *
- * *
- **************************************************************************************}
-
- unit Files;
-
-
-
-
- interface
-
-
-
-
- uses
- Wintypes, WinProcs, CommDlg;
-
-
- type
- TFilename = array[0..255] of Char;
-
-
-
- function LoadBitmap(FileName : PChar; HWindow : HWnd;
- var Width, Height : LongInt; var hCP : hPalette) : HBitmap;
-
- function FileDialog(Owner: HWnd; Filename: PChar; Save: Boolean) : Boolean;
-
-
-
- implementation
-
-
-
-
-
- function FileDialog(Owner: HWnd; Filename: PChar; Save: Boolean) : Boolean;
- const
- DefOpenFilename: TOpenFilename = (
- lStructSize: SizeOf(TOpenFilename);
- hwndOwner: 0;
- hInstance: 0;
- lpstrFilter: 'BMP files (*.BMP)'#0'*.BMP'#0'RLE files (*.RLE)'#0'*.RLE'#0;
- lpstrCustomFilter: nil;
- nMaxCustFilter: 0;
- nFilterIndex: 0;
- lpstrFile: nil;
- nMaxFile: SizeOf(TFilename);
- lpstrFileTitle: nil;
- nMaxFileTitle: 0;
- lpstrInitialDir: nil;
- lpstrTitle: nil;
- Flags: 0;
- nFileOffset: 0;
- nFileExtension: 0;
- lpstrDefExt: 'BMP');
- var
- OpenFilename: TOpenFilename;
- begin
- OpenFilename := DefOpenFilename;
- OpenFilename.hwndOwner := Owner;
- OpenFilename.lpstrFile := Filename;
- if Save then
- begin
- OpenFilename.Flags := ofn_PathMustExist + ofn_NoChangeDir +
- ofn_OverwritePrompt;
- FileDialog := GetSaveFilename(OpenFilename);
- end else
- begin
- OpenFileName.Flags := ofn_PathMustExist;
- FileDialog := GetOpenFilename(OpenFilename);
- end;
- end;
-
-
-
-
- { This kind of imitates POINTER ARITHMETIC }
-
- function AdvancePointer(CurrentPosition : Pointer; NumberOfBytes : Word) : Pointer;
- var
- Seg, Off : Word;
- Adder : LongInt;
- begin
- Seg := Hiword(LongInt(CurrentPosition));
- Off := LoWord(LongInt(CurrentPosition));
-
- Adder := LongInt(Off) + LongInt(NumberOfBytes);
- if (Adder > 65535) then
- begin
- Off := Word(Off + NumberOfBytes);
- Seg := Seg + 8;
- end
- else
- Off := Off + NumberOfBytes;
-
- AdvancePointer := Pointer(Makelong(Off, Seg));
- end;
-
-
-
-
- { Well folks, wanted to give you a different way to do this, other than Borland's.
- Doesn't depend on the MYSTERY function that the other's rely on. }
-
- procedure GetBitmapData(var TheFile : File; BitsHandle : THandle;
- BitsByteSize : LongInt);
- var
- CurrentPosition : Pointer;
- NumberOfBytes : Word;
-
- begin
- CurrentPosition := GlobalLock(BitsHandle);
- while (BitsBytesize > 0) do
- begin
- if (BitsByteSize > 65535) then
- NumberOfBytes := 65535
- else
- NumberOfBytes := BitsByteSize;
-
- BlockRead(TheFile, CurrentPosition^, NumberOfBytes);
- BitsByteSize := BitsByteSize - NumberOfBytes;
- CurrentPosition := AdvancePointer(CurrentPosition, NumberOfBytes);
- end;
- GlobalUnlock(BitsHandle)
- end;
-
-
-
-
- function IsBitmapFile(FileName : PChar; var F : File) : Boolean;
- var
- TestValue : LongInt;
-
- begin
- IsBitmapFile := False;
- Assign(F, FileName);
-
- {$I-}
- Reset(F, 1);
- {$I+}
-
- if (IoResult = 0) then
- begin
- Seek(F, 14);
- BlockRead(F, TestValue, SizeOf(TestValue));
- if (TestValue = $28) then
- IsBitmapFile := True
- else
- Close(F);
- end;
- end;
-
-
-
-
- Procedure CopyDIBPalette(var bmi : TBitMapInfo; var hCP : hPalette);
- var
- LogPal : PLogPalette;
-
- i : Longint;
- PalSize : Longint;
- sz : Longint;
- begin
- PalSize := 1 shl bmi.bmiHeader.biBitCount;
- sz := Sizeof(TLogPalette)+Pred(PalSize)*Sizeof(TPaletteEntry);
- GetMem(LogPal,sz);
- LogPal^.palNumEntries := PalSize;
- LogPal^.palVersion := $0300;
-
- {$R-}
- for i := 0 to Pred(PalSize) do
- begin
- LogPal^.palPalEntry[i].peRed := bmi.bmicolors[i].RGBRed;
- LogPal^.palPalEntry[i].peGreen := bmi.bmicolors[i].RGBGreen;
- LogPal^.palPalEntry[i].peBlue := bmi.bmicolors[i].RGBBlue;
- LogPal^.palPalEntry[i].peflags := 0;
- end;
- {$R+}
- hCP := CreatePalette(LogPal^);
- FreeMem(LogPal,sz);
- end;
-
-
-
-
- function LoadBitmap(FileName : PChar; HWindow : HWnd;
- var Width, Height : LongInt; var hCP : hPalette) : HBitmap;
- var
- BitmapInfo : PBitmapInfo;
- BmpHandle : THandle;
- BitsInPixel : Word;
- HeaderSize : Word;
- LWidth : LongInt;
- PBits : Pointer;
- F : File;
- DC : HDC;
- OldPalette : hPalette;
-
- begin
- LoadBitmap := 0;
- if (IsBitmapFile(FileName, F)) then
- begin
- Seek(F, 28);
- BlockRead(F, BitsInPixel, Sizeof(BitsInPixel));
- if (BitsInPixel <= 8) then
- begin
- HeaderSize := Sizeof(TBitmapInfoHeader) +
- ((1 shl BitsInPixel) * Sizeof(TRGBQuad));
- GetMem(BitmapInfo, HeaderSize);
- if (BitmapInfo <> nil) then
- begin
- with BitmapInfo^, BMIHeader do
- begin
- Seek(F, Sizeof(TBitmapFileHeader));
- BlockRead(F, BitmapInfo^, HeaderSize);
- Width := BIWidth;
- Height := BIHeight;
- CopyDIBPalette(BitmapInfo^, hCP);
- if (BICompression = bi_RGB) then
- begin
- LWidth := (((Width * BitsInPixel)+31) div 32) * 4;
- BISizeImage := LWidth * Height;
- end;
- BmpHandle := GlobalAlloc(gmem_Moveable or gmem_ZeroInit, BISizeImage);
- if (BmpHandle <> 0) then
- begin
- GetBitmapData(F, BmpHandle, BISizeImage);
- PBits := GlobalLock(BmpHandle);
- if (PBits <> nil) then
- begin
- DC := CreateDC('Display', nil, nil, nil);
- PBits := GlobalLock(BmpHandle);
-
- OldPalette := SelectPalette(DC, hCP, FALSE);
- UnrealizeObject(hCP);
- RealizePalette(DC);
-
- LoadBitmap := CreateDIBitmap(DC, BMIHeader, cbm_Init, PBits,
- BitmapInfo^, 0);
-
- SelectPalette(DC, OldPalette, FALSE);
-
- DeleteDC(DC);
- GlobalUnlock(BmpHandle);
- end;
- GlobalFree(BmpHandle);
- end;
- end;
- FreeMem(BitmapInfo, HeaderSize);
- end;
- end;
- Close(F);
- end;
- end;
-
-
-
-
- end.
-